/*
 * PatientList.java
 *
 * Created on __DATE__, __TIME__
 */

package ui;

import java.io.EOFException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import bean.ConnectionBean;

/**
 *
 * @author  __USER__
 */
public class PatientList extends javax.swing.JInternalFrame {

	/** Creates new form PatientList */
	public PatientList() {
		initComponents();
		fillTableData();
	}

	public void fillTableData() {
		try {
			rowData.clear();
			Connection con = ConnectionBean.getConnection();
			PreparedStatement psm = con
					.prepareStatement("select name, phone from patient");
			ResultSet rs = psm.executeQuery();
			while (rs.next()) {
				Vector<String> row = new Vector<String>();
				row.add(rs.getString(1));
				row.add(rs.getString(2));
				rowData.add(row);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		((DefaultTableModel) idUserTable.getModel()).fireTableDataChanged();
		idUserTable.validate();
	}

	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		idUserTable = new javax.swing.JTable();
		idEditPatient = new javax.swing.JButton();
		idShowHistory = new javax.swing.JButton();

		setClosable(true);
		setIconifiable(true);
		setMaximizable(true);
		setResizable(true);
		setTitle("Patient List");
		setAutoscrolls(true);
		setDoubleBuffered(true);
		setMinimumSize(new java.awt.Dimension(700, 500));
		setPreferredSize(new java.awt.Dimension(700, 500));
		setVisible(true);

		jScrollPane1.setPreferredSize(new java.awt.Dimension(550, 502));

		idUserTable.setAutoCreateRowSorter(true);
		idUserTable.setModel(new rituTableModel());
		idUserTable.setDoubleBuffered(true);
		jScrollPane1.setViewportView(idUserTable);

		idEditPatient.setText("Edit Details");
		idEditPatient.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				idEditPatientActionPerformed(evt);
			}
		});

		idShowHistory.setText("Show History");
		idShowHistory.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				idShowHistoryActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addComponent(
												jScrollPane1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												548,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																false)
														.addComponent(
																idEditPatient,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(
																idShowHistory,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addGap(30, 30, 30).addComponent(
						idEditPatient).addPreferredGap(
						javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(idShowHistory).addContainerGap(368,
								Short.MAX_VALUE)).addComponent(jScrollPane1,
				javax.swing.GroupLayout.DEFAULT_SIZE, 466, Short.MAX_VALUE));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void idShowHistoryActionPerformed(java.awt.event.ActionEvent evt) {
		if (isRowSelectionValid()) {
			int selectedRow = idUserTable.getSelectedRow();
			Connection con = ConnectionBean.getConnection();
			try {
				PreparedStatement psm = con
						.prepareStatement("SELECT * FROM sameermehrotra.dignosis where patient_name = ? and phone_no = ?");
				psm.setString(1, idUserTable.getValueAt(selectedRow, 0)
						.toString().trim());
				psm.setString(2, idUserTable.getValueAt(selectedRow, 1)
						.toString().trim());
				ResultSet rs = psm.executeQuery();
				if (rs.next()) {
					String phoneNumber = idUserTable.getValueAt(selectedRow, 1)
							.toString().trim();
					EditHistoryDialog editHistoryDialog = new EditHistoryDialog(
							null, true, phoneNumber);
					editHistoryDialog.setLocationRelativeTo(idUserTable
							.getParent());
					editHistoryDialog.setVisible(true);
				} else {
					JOptionPane.showMessageDialog(this,
							"No Entry found in Dignosis for this patient "
									+ idUserTable.getValueAt(selectedRow, 0)
											.toString().trim());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
	}

	private void idEditPatientActionPerformed(java.awt.event.ActionEvent evt) {
		if (isRowSelectionValid()) {
			int selectedRow = idUserTable.getSelectedRow();
			Connection con = ConnectionBean.getConnection();
			try {
				PreparedStatement psm = con
						.prepareStatement("SELECT * FROM sameermehrotra.patient where name = ? and phone = ?");
				psm.setString(1, idUserTable.getValueAt(selectedRow, 0)
						.toString());
				psm.setString(2, idUserTable.getValueAt(selectedRow, 1)
						.toString());
				ResultSet rs = psm.executeQuery();
				if (rs.next()) {
					EditPatientDialog editPatientDialog = new EditPatientDialog(
							null, true, this);
					editPatientDialog.setLocationRelativeTo(idUserTable
							.getParent());
					editPatientDialog.idName.setText(rs.getString("name"));
					editPatientDialog.idDOB.setValue(rs.getDate("DOB"));
					editPatientDialog.idPhone.setText(rs.getString("phone"));
					editPatientDialog.idAddress
							.setText(rs.getString("address"));
					editPatientDialog.idPlace.setText(rs.getString("place"));
					editPatientDialog.idSex
							.setSelectedItem(rs.getString("sex"));
					editPatientDialog.oldPhoneNumber = rs.getString("phone")
							.trim();
					editPatientDialog.setVisible(true);

				} else {
					JOptionPane.showMessageDialog(this,
							"No Entry found in User Table");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private boolean isRowSelectionValid() {
		int rows = idUserTable.getSelectedRowCount();
		if (rows > 1)
			JOptionPane.showMessageDialog(this,
					"Please select only one row to Edit");
		else if (rows < 1)
			JOptionPane
					.showMessageDialog(this, "Please select one row to Edit");
		else
			return true;
		return false;
	}

	Vector<String> columns = new Vector<String>();
	Vector<Vector<String>> rowData = new Vector<Vector<String>>();

	class rituTableModel extends DefaultTableModel {
		public rituTableModel() {
			columns.addElement("Name");
			columns.addElement("ID/Ph.No.");
		}

		@Override
		public Class<?> getColumnClass(int columnIndex) {
			return String.class;
		}

		@Override
		public int getColumnCount() {
			return columns.size();
		}

		@Override
		public String getColumnName(int columnIndex) {
			return columns.elementAt(columnIndex);
		}

		@Override
		public int getRowCount() {
			return rowData.size();
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			return rowData.elementAt(rowIndex).elementAt(columnIndex);
		}

		@Override
		public boolean isCellEditable(int rowIndex, int columnIndex) {
			return true;
		}

		@Override
		public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
			rowData.elementAt(rowIndex).setElementAt(String.valueOf(aValue),
					columnIndex);
		}

		@Override
		public void fireTableDataChanged() {
			super.fireTableDataChanged();
		}

	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton idEditPatient;
	private javax.swing.JButton idShowHistory;
	private javax.swing.JTable idUserTable;
	private javax.swing.JScrollPane jScrollPane1;
	// End of variables declaration//GEN-END:variables

}